﻿@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
}
@section head{
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/jquery.metadata.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Content/roohui/jquery-validation/messages_cn.js")" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
           
            $("#guaranteeCompany").ligerComboBox(
                {
                    url: '/GuarCompany/GetGuarCompanyListByKey',
                    valueField: 'ID',
                    textField: 'CompanyName',
                    autocomplete: true,
                    initValue: -1,
                    initText: "全部",
                    width: 250
                }
            );
            $("#txtKey").ligerTextBox({ width: 300 });
            
        });
    </script>
}
<table cellpadding="0" cellspacing="0" class="l-table-edit">
    <tr>
        <td align="right" class="l-table-edit-td">
            关键字:
        </td>
        <td align="left" class="l-table-edit-td">

            <input name="txtKey" id="txtKey" ltype="text"  ligerui="width:300"
                style="width: 300;"/>
        </td>
        <td align="left">
        </td>
        <td align="right" class="l-table-edit-td">
            担保公司:
        </td>
        <td align="left" class="l-table-edit-td">
            <input type="text" id="guaranteeCompany" />
        </td>
        <td align="left" class="l-table-edit-td">
            <input type="button" value="查询" id="btnSearch" onclick="search()" class="l-button" />
        </td>
    </tr>
</table>
<div id="projectgrid">
</div>
<form action="/" method="post" id="frmPublish" name="frmPublish" style="background-color: white;display:none">
 
    <table cellpadding="0" cellspacing="0" class="l-table-edit" width="600">
        <tr>
            <td align="right" class="l-table-edit-td">
                <strong>
                    <h3>
                        开户信息</h3>
                </strong>
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" width="100">
                <span class="l-star">*</span>&nbsp;用户名:
            </td>
            <td align="left" class="l-table-edit-td" width="200">
                <input name="txtAgentName" type="text" id="txtAgentName" validate="{required:true}" />
            </td>
            <td align="right" class="l-table-edit-td" width="100">
                <span class="l-star">*</span>&nbsp;真实姓名:
            </td>
            <td align="left" class="l-table-edit-td" width="200">
                <input name="txtRealName" type="text" id="txtRealName" validate="{required:true}" />
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;身份证号:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtAgentIDNO" type="text" id="txtAgentIDNO" validate="{required:true}" />
            </td>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;手机号码:
            </td>
            <td align="left" class="l-table-edit-td">
                <input name="txtAgentHandPhone" type="text" id="txtAgentHandPhone" validate="{required:true}" />
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <span class="l-star">*</span>&nbsp;居住地址:
            </td>
            <td align="left" class="l-table-edit-td" colspan="3">
                <input name="txtAddress" type="text" id="txtAddress" ligerui="width:490" style="width: 490"
                    validate="{required:true}" />
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td">
                <strong>
                    <h3>
                        时间控制</h3>
                </strong>
            </td>
        </tr>
        <tr>
            <td align="right" class="l-table-edit-td" width="100">
                <span class="l-star">*</span>&nbsp;发布时间:
            </td>
            <td align="left" class="l-table-edit-td" width="200">
            <input type="text" id="txtReleaseTime" validate="{required:true}"/> 
            </td>
            <td align="right" class="l-table-edit-td" width="100">
                <span class="l-star">*</span>&nbsp;投资起息日:
            </td>
            <td align="left" class="l-table-edit-td" width="200">
                <input name="txtBeginDate" type="text" id="txtBeginDate" ltype="date" validate="{required:true}" />
            </td>
        </tr>
         <tr>
            <td align="right" class="l-table-edit-td" width="100">
                 
            </td>
            <td align="left" class="l-table-edit-td" width="200">
                 
            </td>
            <td align="right" class="l-table-edit-td" width="100">
                <span class="l-star">*</span>&nbsp;借款计息日:
            </td>
            <td align="left" class="l-table-edit-td" width="200">
                <input name="txtBorrowInterestDate" type="text" id="txtBorrowInterestDate" ltype="date" validate="{required:true}" />
            </td>
        </tr>
        <tr>
            <td align="center" colspan="4">
                 <span id="lblmsg" class="l-validate-error">&nbsp;</span>
            <input type="hidden" name="projectID" id="projectID" value="0" />
            </td>
        </tr>
    </table>
 
</form>
<script type="text/javascript">
    var projectgrid;
    var saveDialog = null;
    $(function () {
        $("#txtReleaseTime").ligerDateEditor({ showTime: true });
        
        projectgrid = $("#projectgrid").ligerGrid({
            columns: [

                { display: '项目编码', name: 'ProjectCode', width: '10%', isSort: false },
                {
                    display: '项目名称', isAllowHide: false, width: '15%', isSort: false,
                    render: function (row) {

                        var html = '<a href="#" onclick="grid_project_view(\'' + row.ID + '\')">' + row.ProjectTitle + '</a>';

                        return html;
                    }
                },

                { display: '产品名称', name: 'ProductName', width: '12%', isSort: false },
                { display: '借款企业', name: 'EnterpriseName', width: '15%', isSort: false,
                    render: function (item) {
                        return item.Enterprise.EnterpriseName;
                    }
                },

                { display: '借款金额', name: 'CycleAmount', width: '12%', isSort: false,
                    render: function (item) {
                        return Number(item.Financing / 10000).toFixed(2) + "万元";
                    }
                },
                { display: '借款时长', name: 'CycleAmount', width: '8%', isSort: false,
                    render: function (item) {
                        var durationType = item.Policy.DurationType == 0 ? "天" : "月";
                        return (item.CycleAmount * item.Policy.Duration) + durationType;
                    }
                },
                { display: '担保公司', name: 'CompanyName', width: '10%', isSort: false,
                    render: function (item) {
                        return item.GuaranteeCompany.CompanyName;
                    }
                },

                { display: '状态', name: 'Status', width: '8%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.Status) == 3) return '审核已通过';
                        return "";
                    }
                },
                {
                    display: '操作', isAllowHide: false,
                    render: function (row) {

                        var html = '<a href="#" onclick="grid_project_publish(\'' + row.ID + '\')">发布</a>';

                        return html;
                    }
                }
                ],
            width: '100%', height: '100%', rownumbers: true,
            dataAction: 'server',
            url: "/ProjectPublish/GetProjectListForPublish",
            pageSizeOptions: [10],
            pageSize: 10
        });


        $.metadata.setType("attr", "validate");
        var v = $("#frmPublish").validate({
            //debug: true,
            errorPlacement: function (lable, element) {
                if (element.hasClass("l-textarea")) {
                    element.ligerTip({ content: lable.html(), target: element[0] });
                }
                else if (element.hasClass("l-text-field")) {
                    element.parent().ligerTip({ content: lable.html(), target: element[0] });
                }
                else {
                    lable.appendTo(element.parents("td:first").next("td"));
                }
            },
            success: function (lable) {
                lable.ligerHideTip();
                lable.remove();
            },
            submitHandler: function () {
                var mobilereg = /^1[3|5|8]\d{9}$/;
                var idcardreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
                var emailreg = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z0-9]{2,3}$/;
                if ($('#txtAgentName').val() == "") {
                    $('#txtAgentName').focus();
                    $("#lblmsg").html('用户名不能为空');
                    return;
                }
                if (!emailreg.test($('#txtAgentName').val())) {
                    $('#txtAgentName').focus();
                    $("#lblmsg").html('用户名格式错误');
                    return;
                }
                if ($('#txtRealName').val() == "") {
                    $('#txtRealName').focus();
                    $("#lblmsg").html('真实姓名不能为空');
                    return;
                }
                if ($('#txtAgentIDNO').val() == "") {
                    $('#txtAgentIDNO').focus();
                    $("#lblmsg").html('身份证号码不能为空');
                    return;
                }
                if (!idcardreg.test($('#txtAgentIDNO').val())) {
                    $('#txtAgentIDNO').focus();
                    $("#lblmsg").html('身份证号码格式错误');
                    return;
                }
                if ($('#txtAgentHandPhone').val() == "") {
                    $('#txtAgentHandPhone').focus();
                    $("#lblmsg").html('手机不能为空');
                    return;
                }
                if (!mobilereg.test($('#txtAgentHandPhone').val())) {
                    $('#txtAgentHandPhone').focus();
                    $("#lblmsg").html('手机号码输入错误');
                    return;
                }
                if ($('#txtAddress').val() == "") {
                    $('#txtAddress').focus();
                    $("#lblmsg").html('居住地址不能为空');
                    return;
                }

                if ($('#txtReleaseTime').val() == "") {
                    $('#txtReleaseTime').focus();
                    $("#lblmsg").html('发布时间不能为空');
                    return;
                }
                if ($('#txtBeginDate').val() == "") {
                    $('#txtBeginDate').focus();
                    $("#lblmsg").html('投资起息日不能为空');
                    return;
                }
                if ($('#txtBorrowInterestDate').val() == "") {
                    $('#txtBorrowInterestDate').focus();
                    $("#lblmsg").html('借款计息日不能为空');
                    return;
                }
                $.ajax({
                    url: "/ProjectPublish/ProjectPublish",
                    data: "projectID=" + $("#projectID").val() + "&userName=" + $('#txtAgentName').val() + "&realName=" + $("#txtRealName").val()
                    + "&idCardNo=" + $('#txtAgentIDNO').val() + "&handPhone=" + $("#txtAgentHandPhone").val()
                    + "&address=" + $("#txtAddress").val()
                    + "&releaseTime=" + $("#txtReleaseTime").val()
                    + "&beginDate=" + $("#txtBeginDate").val()
                    + "&borrowInterestDate=" + $("#txtBorrowInterestDate").val(),
                    type: "post",
                    success: function (result) {
                        $("#projectID").val("0");
                        //写提交ajax操作
                        if (result == "1") {
                            $.ligerDialog.success("项目发布成功。"); 
                            saveDialog.hide();
                            search();
                        }
                        else { 
                                $.ligerDialog.error("项目发布失败。");
                             
                        }

                    }
                });
            }
        });
        $("#frmPublish").ligerForm();
        
    });
    function search() {
        var guaranteeCompanyID = liger.get("guaranteeCompany").getValue();
        if (guaranteeCompanyID == "") {
            guaranteeCompanyID = "-1";
        }
        projectgrid.setOptions(
             {
                 parms:
                [
                    { name: "key", value: $("#txtKey").val() },
                    { name: "guaranteeCompanyID", value: guaranteeCompanyID },
                    { name: "page", value: 1 }
                ]
             });
        projectgrid.loadData();
    }
     
    function grid_project_publish(projectID) {
        //location.href = decodeURIComponent("/ProjectPublish/ProjectPublish?projectID=" + projectID);

        $("#projectID").val(projectID);
        saveDialog = $.ligerDialog.open({
            title: "项目发布",
            id: 'publishDialog',
            name: 'publishDialog',
            slide: false,
            isHidden: false,
            modal: true,
            target: $("#frmPublish"),
            width: 630, isResize: true, allowClose: false,
            buttons: [
                    { text: '发布', onclick: project_publish },
                    {
                        text: '取消', onclick: function (item, dialog) {
                            dialog.hide();
                            $("#projectID").val("0");
                        }
                    }
                ]
        });

            }
            function project_publish() {
                var form = $('#frmPublish');
                form.submit();
            }
    
    function grid_project_view(projectID) {
        top.$.ligerDialog.open(
        { title:'项目相关信息',height: 500, width: 1024,
            url: '/Project/ProjectView?projectID=' + projectID,
            buttons: [{ text: '关闭', onclick: function (item, dialog) { dialog.close(); } }]
        });
    }
</script>
